<?php

/**
 * @author Grover Campos
 * @version 1.0
 * @created 03-ago-2009 06:43:07 p.m.
 */
class Gtux_Model_Gateway_Subformulario extends Gtux_Model_Gateway_Abstract 
{
  /**
   * Devuelve la tabla 
   *
   * @return Gtux_Model_Table_Subformulario
   */
	public function getDbTable()
	{
    if (null == $this->_dbTable) 
    {
      $this->setDbTable('Gtux_Model_Table_Subformulario');
    }
    return $this->_dbTable;
	}
	
	/**
	 * 
	 * @param Gtux_Model_Subformulario $subformulario
	 * @return int
	 */
	public function delete(Gtux_Model_Abstract $subformulario)
	{
	  $adapter = $this->getDbTable()->getAdapter();
	  $where  = $adapter->quoteInto('id_formulario = ?', $subformulario->getIdFormulario());
    $where .= ' AND '.$adapter->quoteInto('id_subformulario = ?', $subformulario->getIdSubformulario());
    return $this->getDbTable()->delete($where);
	}

	/**
	 *
	 * @param int $id_subformulario
	 * @param Gtux_Model_Subformulario $subformulario
	 */
	public function find($id_subformulario, Gtux_Model_Abstract $subformulario)
	{
	  if (Zend_Validate::is($subformulario->getIdFormulario(), 'Int')) 
	  {
	  	$rs = $this->getDbTable()->find(
	  	  $subformulario->getIdFormulario(),
	  	  $id_subformulario
	  	);
	  	
	  	if (0 == count($rs)) 
	  	{
	  		return;
	  	}
      $row      = $rs->current();
      $rowArray = $row->toArray();
      foreach ($rowArray as $column => $value) 
      {
        $subformulario->$column = $value;
      }           
	  }
	  
	}

	public function fetchAll()
	{
    $rs      = $this->getDbTable()->fetchAll();
    $entries = array();
    foreach ($rs as $row) 
    {
      $entries[] = new Gtux_Model_Subformulario(null, $row);
    }
    return $entries;
	}

	/**
	 * 
	 * @param Gtux_Model_Subformulario $subformulario
	 */
	public function insert(Gtux_Model_Abstract $subformulario)
	{
    $data = array(
      'id_formulario' => $subformulario->getIdFormulario(),
      'nombre'        => $subformulario->getNombre(),
      'etiqueta'      => $subformulario->getEtiqueta(),
      'id_user'       => $subformulario->getIdUser(),
      'created_at'    => $subformulario->getCreatedAt()->toString(Zend_Date::ISO_8601)
    );
    $id = $subformulario->getIdSubformulario();
    if (NULL == $id) 
    {
    	return $this->getDbTable()->insert($data);
    }
    else
    {
      $this->_logger->err('Se intento insertar un subformulario con id');
      throw new Zend_Db_Exception('Se intento insertar un subformulario con id');
    }
	}
	
  /**
   * Update subformulario
   * @see library/Gtux/Model/Gateway/Gtux_Model_Gateway_Abstract#update($model)
   */
	public function update(Gtux_Model_Abstract $subformulario)
	{
	  $data = array();
	  if ($subformulario->getIdFormulario() > 0) 
	  {
	  	$data['id_formulario'] = $subformulario->getIdFormulario();
	  }
	  if (strlen($subformulario->getNombre())>0) 
	  {
	  	$data['nombre'] = $subformulario->getNombre();
	  }
	  if (strlen($subformulario->getEtiqueta())>0) 
	  {
	  	$data['etiqueta'] = $subformulario->getEtiqueta();
	  }
	  $id = $subformulario->getIdSubformulario();
	  if (NULL !== $id && sizeof($data)>0) 
	  {
      $adapter = $this->getDbTable()->getAdapter();
      $where  = $adapter->quoteInto('id_formulario = ?', $subformulario->getIdFormulario());
      $where .= ' AND '.$adapter->quoteInto('id_subformulario = ?', $subformulario->getIdSubformulario());
      return $this->getDbTable()->update($data, $where);
	  }
    else
    {
      $this->_logger->err('Se intento actualizar un subformulario sin id');
      throw new Zend_Db_Exception('Se intento actualizar un subformulario sin id');
    }
	}

  /**
   * @see Gtux_Model_Gateway_Abstract::save()
   *
   * @param Gtux_Model_Abstract $model
   */
  public function save(Gtux_Model_Abstract $subformulario)
  {
    $id = $subformulario->getIdSubformulario();
    if (Zend_Validate::is($subformulario->getIdFormulario(), 'Int'))
    {
      if (NULL == $id) 
      {
        $db = $this->getDbTable()->getAdapter();
        $db->beginTransaction();
        $flg = true;
        
        try
        {                  
          $id = $this->insert($subformulario);
          $subformulario->setIdSubformulario($id['id_subformulario']);
          if ($subformulario->getCampos()->count() > 0) 
          {
            foreach ($subformulario->getCampos() as $campo) 
            {
              $campo->setSubformulario($subformulario);
              $flg &= (bool)$campo->save();
            }
          }
          if ($flg)
          {
            $db->commit();
            return true;
          }
          else
          {
            $db->rollBack();
            $this->_logger->err('Error al guardar el subformulario');
            return false;
          }
        }
        catch (Exception $e)
        {
          $db->rollBack();
          $this->_logger->err($e->getMessage());
          return false;
        }
      }
      else 
      {
        return $this->update($subformulario);
      }    
    }
    return false;
  }
	
}
?>